package petstore.persistence;

import petstore.domain.Comment;
import petstore.persistence.impl.CommentDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class CommentDaoImpl implements CommentDao
{
    private static final String GET_COMMENT_BY_ITEMNAME = "SELECT username,comment,published_time FROM comment WHERE item_name = ?";
    private static final String FIND_COMMENT_BY_USERNAME = "SELECT comment FROM comment WHERE username = ? AND item_name = ?";
    private static final String INSERT_COMMENT_BY_USERNAME = "INSERT INTO comment (username,comment,published_time,item_name) VALUES (?,?,?,?)";
    private static final String DELETE_COMMENT_BY_USERNAME_AND_COMMENT_AND_ITEMNAME = "DELETE from comment WHERE username = ? AND item_name = ? AND comment = ?";
    @Override
    public List<Comment> getCommentByUsernameAndItemname(String item_name) {
        Comment comment = null;
        List<Comment> commentList = new ArrayList<>();
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(GET_COMMENT_BY_ITEMNAME);
            preparedStatement.setString(1,item_name);
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next())
            {
                comment = new Comment();
                comment.setComment(resultSet.getString("comment"));
                comment.setPublished_time(resultSet.getString("published_time"));
                comment.setUsername(resultSet.getString("username"));
                commentList.add(comment);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return commentList;
    }

    //防止评论重复的函数
    @Override
    public boolean findCommentByUsername(String username,String comment,String itemname) {
        boolean isCommentedRepeatedly = false;
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(FIND_COMMENT_BY_USERNAME);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,itemname);
            ResultSet resultSet = preparedStatement.executeQuery();

            while(resultSet.next())
            {
                System.out.println("原评论为： " + resultSet.getString("comment"));
                System.out.println("添加的评论为： " + comment);
                if(resultSet.getString("comment").equals(comment))
                {
                    isCommentedRepeatedly = true;
                    break;//评论了重复的评论
                }
                else isCommentedRepeatedly = false;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return isCommentedRepeatedly;
    }

    @Override
    public void insertCommentByUsername(String username, String comment,String published_time,String item_name) {
        try
        {
            System.out.println("评论了相同评论： " + findCommentByUsername(username,comment,item_name));
            if(!findCommentByUsername(username,comment,item_name)) //用户之前没有评论过相同的评论
            {
                Connection connection = DBUtil.getConnection();
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_COMMENT_BY_USERNAME);
                preparedStatement.setString(1,username);
                preparedStatement.setString(2,comment);
                preparedStatement.setString(3,published_time);
                preparedStatement.setString(4,item_name);
                preparedStatement.executeUpdate();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public boolean deleteCommentByUsernameAndItemnameAndComment(String username, String comment, String item_name) {
        try
        {
            Connection connection = DBUtil.getConnection();
            PreparedStatement preparedStatement = connection.prepareStatement(DELETE_COMMENT_BY_USERNAME_AND_COMMENT_AND_ITEMNAME);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,item_name);
            preparedStatement.setString(3,comment);
            preparedStatement.executeUpdate();
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }
}
